<?php
if($_REQUEST['run'] =='alarm'){
/*action when click button Go*/
if(isset($_POST['save']))
{
	$pause = $_POST['pause'];
	array_map('intval',$pause);
	$pause = implode(',',$pause);
	$pieces = explode(",", $pause);
	$action =$_POST['action'];
	if($action=='Pause'){
		$file = 'plugins/nms/log_nms/log_nms.txt';
		// Open the file to get existing content
		$current = file_get_contents($file);
		$user_id= db_fetch_cell("select id from user_auth where id=" . $_SESSION["sess_user_id"]);
		$user_name = db_fetch_cell("SELECT full_name from user_auth where id ='$user_id'");
		$today = date("Y-m-d H:i:s");
		// Append a new person to the file
		for($x=0;$x<count($pieces);$x++){
			$interface_id=(int)$pieces[$x];
			$interface_name = db_fetch_cell("SELECT CONCAT('(',interface_name,')','\t',interface_alias) as interface_des from plugin_nms_interface where interface_id ='$interface_id'");
			$current .= $today. " NETWORK MONITORING SYSTEM : Interface [".$interface_name."] be paused by user :".$user_name." \n";
		}
		// Write the contents back to the file
		file_put_contents($file, $current);
		update_action_pause($pause);
	}
	elseif ($action=='Resume'){
		$file = 'plugins/nms/log_nms/log_nms.txt';
		// Open the file to get existing content
		$current = file_get_contents($file);
		$user_id= db_fetch_cell("select id from user_auth where id=" . $_SESSION["sess_user_id"]);
		$user_name = db_fetch_cell("SELECT full_name from user_auth where id ='$user_id'");
		$today = date("Y-m-d H:i:s");
		//update_action_resume($pause);
		$hostarray  = array();
		$string ="";
		$check =0;
		for($x=0;$x<count($pieces);$x++)
		{
		$interface_id=(int)$pieces[$x];
		//$interface_id=(int)$pieces[$x];
		$interface_name = db_fetch_cell("SELECT CONCAT('(',interface_name,')','\t',interface_alias) as interface_des from plugin_nms_interface where interface_id ='$interface_id'");
		$current .= $today. " NETWORK MONITORING SYSTEM : Interface [".$interface_name."] be unpaused by user :".$user_name." \n";
				// Write the contents back to the file
				file_put_contents($file, $current);
		}
		for($x=0;$x<count($pieces);$x++)
		{
		$interface_id=(int)$pieces[$x];
		$host = db_fetch_row('Select a.id, a.hostname,a.snmp_community, b.status_oid,b.alias_oid from host a, plugin_nms_interface b
										where a.id = b.host_id and b.interface_id = '.$interface_id.' and b.nms_pause = 0');
												if(!empty($host)){
												$check =1;
												update_action_resume($pause);
												if(!in_array($host['hostname'],$hostarray)){
												$hostarray[] = $host['hostname'];
												}
												// get status
												$status = snmp2_get($host['hostname'],$host['snmp_community'],$host['status_oid']);
												$tem = substr($status, -2,1);
												if($tem == 1){
												$status = "Up";
												}else{
												if($tem == 2){
												$status = "Down";
												}else{
												$status = "Unknown";
												}
												}
												// get alias
												$alias = snmp2_get($host['hostname'],$host['snmp_community'],$host['alias_oid']);
												$alias = str_replace('STRING: ', '', $alias);
														$alias = trim($alias, '" "');
														// update alias into nms, plugin_nms_interface
														update_alias_interface($alias, $interface_id);
														//update status into cacti, host_snmp_cache
														update_field_value_host_snmp_cache($status,$host['id'],$item1['status_oid']);
														//update alias into cacti, host_snmp_cache
														update_field_value_host_snmp_cache($alias,$host['id'],$item1['alias_oid']);
												}

		}
		$alert="";
		for($i=0;$i<count($hostarray);$i++){
			$string = $string . "" . $hostarray[$i] ."". " , ";

		}

		if($check ==1){
			echo'
				<script>
						alert("Update information successfull, please querry host:  '.$string.'");
					</script>';
		}else{
			echo'
					<script>
					alert("Interface status is not pause '.$check.'");
						</script>';
		}
	}
	elseif ($action=='Active'){
		for($x=0;$x<count($pieces);$x++)
		{
		$interface_id= (int)$pieces[$x];
		update_log($interface_id);
		update_isound($interface_id);
		}
		}
		echo '<script>
		reLoad();
		</script>';
			exit();
		}
}
?>